package org.boot.easyapi.pojo.web;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 通用返回
 *
 * @param <T> 数据泛型
 */
@Data
@Getter
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    /**
     * 错误码
     *
     */
    @JsonProperty("code")
    private Integer code;
    /**
     * 返回数据
     */
    @JsonProperty("data")
    private T data;
    /**
     * 错误提示，用户可阅读
     *
     */
    @JsonProperty("msg")
    private String msg;

    @JsonProperty("time")
    private LocalDateTime time;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = 0;
        result.msg = "";
        result.data = data;
        result.time = LocalDateTime.now();
        return result;
    }

    public static <T> CommonResult<T> success(T data, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.code = 0;
        result.msg = msg;
        result.data = data;
        result.time = LocalDateTime.now();
        return result;
    }



    // 错误响应
    public static <T> CommonResult<T> error(int code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = msg;
        result.data = null;
        result.time = LocalDateTime.now();
        return result;
    }


}
